#include "bullectscreenitem.h"
#include <QHBoxLayout>
BullectScreenItem::BullectScreenItem(QWidget *parent)
    : QFrame{parent}
{
    setObjectName("bullectScreenWnd");
    setFixedHeight(38);

    // 弹幕文本 和 用户头像处在水平布局器中
    QHBoxLayout* layout = new QHBoxLayout(this);
    setLayout(layout);

    // 用户头像
    imageLabel = new QLabel(this);
    imageLabel->setFixedSize(30, 30);
    imageLabel->setScaledContents(true);
    imageLabel->hide();

    // 弹幕中⽂本
    textLabel = new QLabel(this);
    textLabel->setStyleSheet("font-family : 微软雅⿊;"
                        "font-size : 18px;"
                        "color : #FFFFFF;"
                        "border : none;");
    textLabel->adjustSize();

    // 将元素添加到⽔平布局器中
    layout->addWidget(imageLabel);
    layout->addWidget(textLabel);
    layout->setContentsMargins(10, 4, 19, 4);
    layout->setSpacing(8);
}

void BullectScreenItem::setBullectScreenText(const QString& text)
{
    textLabel->setText(text);
    textLabel->adjustSize();
}

void BullectScreenItem::setBullectScreenIcon(QPixmap& pixmap)
{
    imageLabel->setPixmap(pixmap);
    imageLabel->show();
    this->setStyleSheet("#bullectScreenWnd{"
                        "border : 1px solid #3ECEFE;"
                        "border-radius : 19px;}");
}

void BullectScreenItem::setBullectScreenAnimation(int xPos, int duration)
{
    animation = new QPropertyAnimation(this, "pos", this);
    animation->setDuration(duration);
    animation->setStartValue(QPoint(xPos, 0));
    animation->setEndValue(QPoint(0 - this->width(), 0));

    connect(animation, &QPropertyAnimation::finished, this, [=](){
        delete this;
    });
}

void BullectScreenItem::startAnimation()
{
    show();
    animation->start();
}
